//
//  MyLayoutOfJokes.m
//  SmileRead
//
//  Created by dllo on 15/8/22.
//  Copyright (c) 2015年 JIZ. All rights reserved.
//

#import "MyLayoutOfJokes.h"



@interface MyLayoutOfJokes ()

{
    // 定义成员变量, 计算最大高度
    CGFloat _sumOfheight;
}



// 装item的属性
@property (nonatomic, retain)NSMutableArray *arrayOfAtt;

@end

@implementation MyLayoutOfJokes

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.dataArray = [NSMutableArray array];
        self.arrayOfAtt = [NSMutableArray array];
    }
    return self;
}

- (void)dealloc
{
    [super dealloc];
}


// 前期准备
- (void)prepareLayout
{
    [super prepareLayout];
    
    [self prepare];
}

- (void)prepare
{
    // 获得指定section的cell个数
    NSInteger num = [self.collectionView numberOfItemsInSection:0];
    for (int i = 0; i < num; i++) {
        
        // 获得指定位置的cell的属性
        NSIndexPath *index = [NSIndexPath indexPathForItem:i inSection:0];
        UICollectionViewLayoutAttributes *att = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:index];
        // 设置属性
        att.frame = CGRectMake(i * self.collectionView.frame.size.width + 10 * widthFit, 20 * heightFit, self.collectionView.frame.size.width - 20 * widthFit, self.collectionView.frame.size.height - 40 * heightFit);
        [self.arrayOfAtt addObject:att];
        
        // 计算collectionView的contentSize.
        _sumOfheight = self.collectionView.frame.size.width;
    }
}

// 返回在collectionView上的cell的属性
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
    NSMutableArray *currentArr = [NSMutableArray array];
    for (UICollectionViewLayoutAttributes *att in self.arrayOfAtt) {
        
        if (CGRectIntersectsRect(att.frame, rect) || CGRectContainsRect(att.frame, rect)) {
            [currentArr addObject:att];
        }
    }
    return currentArr;
}

// 返回单个cell的属性
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return self.arrayOfAtt[indexPath.item];
}

- (CGSize)collectionViewContentSize
{
    return CGSizeMake(self.collectionView.frame.size.width * self.dataArray.count, 0);
}




@end
